﻿/* 
 * BtCommLib - the OpenWebNet .NET Library
 *
 * Copyright (C) 2011 BTicino S.p.A.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using NLog;
using System.Diagnostics;

namespace BTicino.BtCommLib.Trace
{

    /// <summary>
    /// Singleton class to manage library debug traces
    /// </summary>
    public class BTLibLogger
    {

        #region Data memebers

        /// <summary>
        /// Private attribute logger
        /// </summary>
        private Logger _nlogger;

        #endregion

        #region Private constructor

        /// <summary>
        /// Private constructor, create new istance of BTLibLogger.
        /// </summary>
        /// <param name="nlogger">Istance of logger</param>
        private BTLibLogger(Logger nlogger)
        
        {
            this._nlogger = nlogger;
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Public static method to retrieve logger instance, if it's call for the first
        /// time, create new istance of logger
        /// </summary>
        /// <param name="classToLog"></param>
        /// <returns></returns>
        public static BTLibLogger createLogger(string classToLog)
        {
            Logger nlogger = LogManager.GetLogger(classToLog);

            return new BTLibLogger(nlogger);
        }

        /// <summary>
        /// Write a trace, with TRACE level, into logger
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        public void Trace (string msg) 
        {
            Log(LogLevel.Trace, msg, false);
        }

        /// <summary>
        /// Write a trace, with TRACE level, into logger. Condition by advancedMsgFormatting
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        /// <param name="advancedMsgFormatting">Conditional vlaue, if TRUE the message is formatting else not</param>
        public void Trace(string msg, bool advancedMsgFormatting)
        {
            Log(LogLevel.Trace, msg, advancedMsgFormatting);
        }

        /// <summary>
        /// Write a trace, with DEBUG level, into logger
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        public void Debug (string msg) 
        {
            Log(LogLevel.Debug, msg, false);
        }

        /// <summary>
        /// Write a trace, with DEBUG level, into logger. Condition by advancedMsgFormatting
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        /// <param name="advancedMsgFormatting">Conditional vlaue, if TRUE the message is formatting else not</param>
        public void Debug(string msg, bool advancedMsgFormatting)
        {
            Log(LogLevel.Debug, msg, advancedMsgFormatting);
        }

        /// <summary>
        /// Write a trace, with INFO level, into logger
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        public void Info (string msg) 
        {
            Log(LogLevel.Info, msg, false);
        }

        /// <summary>
        /// Write a trace, with INFO level, into logger. Condition by advancedMsgFormatting
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        /// <param name="advancedMsgFormatting">Conditional vlaue, if TRUE the message is formatting else not</param>
        public void Info(string msg, bool advancedMsgFormatting)
        {
            Log(LogLevel.Info, msg, advancedMsgFormatting);
        }

        /// <summary>
        /// Write a trace, with WARNING level, into logger
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        public void Warn (string msg) 
        {
            Log(LogLevel.Warn, msg, false);
        }

        /// <summary>
        /// Write a trace, with WARING level, into logger. Condition by advancedMsgFormatting
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        /// <param name="advancedMsgFormatting">Conditional vlaue, if TRUE the message is formatting else not</param>
        public void Warn(string msg, bool advancedMsgFormatting)
        {
            Log(LogLevel.Warn, msg, advancedMsgFormatting);
        }

        /// <summary>
        /// Write a trace, with ERROR level, into logger
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        public void Error (string msg) 
        {
            Log(LogLevel.Error, msg, false);
        }

        /// <summary>
        /// Write a trace, with ERROR level, into logger. Condition by advancedMsgFormatting
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        /// <param name="advancedMsgFormatting">Conditional vlaue, if TRUE the message is formatting else not</param>
        public void Error(string msg, bool advancedMsgFormatting)
        {
            Log(LogLevel.Error, msg, advancedMsgFormatting);
        }

        /// <summary>
        /// Write a trace, with FATAL level, into logger
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        public void Fatal (string msg) 
        {
            Log(LogLevel.Fatal, msg, false);
        }

        /// <summary>
        /// Write a trace, with FATAL level, into logger. Condition by advancedMsgFormatting
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        /// <param name="advancedMsgFormatting">Conditional vlaue, if TRUE the message is formatting else not</param>
        public void Fatal(string msg, bool advancedMsgFormatting)
        {
            Log(LogLevel.Fatal, msg, advancedMsgFormatting);
        }

        /// <summary>
        /// Trace log to _nlogger instance. Condition by advancedMsgFormatting
        /// </summary>
        /// <param name="level">Log level</param>
        /// <param name="msg">Message to be trace</param>
        /// <param name="advancedMsgFormatting">Conditional vlaue, if TRUE the message is formatting else not</param>
        private void Log(LogLevel level, string msg, bool advancedMsgFormatting)
        {
            if (advancedMsgFormatting)
                msg = FormatMsg(msg);

            _nlogger.Log(level, msg);
        }

        /// <summary>
        /// Formatting message adding some information like clock time, file name and line numeber of the trace
        /// </summary>
        /// <param name="msg">Message to be trace</param>
        /// <returns>Message to be trace formmatting</returns>
        private string FormatMsg(string msg)
        {
            StackFrame CallStack = new StackFrame(2, true);

            // This will report the line number and file name of the original Call!
            string text = msg + ", File: " + CallStack.GetFileName()
                + ", Line: " + CallStack.GetFileLineNumber();

            return text;
        }

        #endregion
    }
}
